﻿using MoviesWeb.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MoviesWeb.DAL
{
   public class MoviesDAL
    {
        static MoviesWebEntities1 MV = new MoviesWebEntities1();
        DbSet<Movies> table = MV.Movies;
        public Movies  IdGetMovies(int id)
        {
           return table.FirstOrDefault(p=>p.Mv_id==id);
        }
        public bool AddMovie(Movies movice)
        {
            table.Add(movice);
           return MV.SaveChanges() == 1;
        }
        public  bool  DeleteMovie(int  id)
        {
            table.Remove(IdGetMovies(id));
            return MV.SaveChanges()==1;
        }
        public bool ChangeMovie(Movies movice)
        {
            var record = IdGetMovies(movice.Mv_id);
               record.Mv_click = movice.Mv_click;
               record.Mv_downaddress = movice.Mv_downaddress;
               record.Mv_headpic = movice.Mv_headpic;
               record.Mv_introducepic = movice.Mv_introducepic;
               record.Mv_name = movice.Mv_name;
               record.Mv_type = movice.Mv_type;
               record.Mv_uptime = movice.Mv_uptime;
               record.Mv_wordintroduce = movice.Mv_wordintroduce;
           return MV.SaveChanges() == 1;
        }
        public List<Movies> TypeGetMovies(string type)
        {
            return table.Where(p => p.Mv_type == type).OrderBy(p => p.Mv_uptime).ToList();
        }
        public List<Movies> KeyWordGetMovies(String key)
        { 
            //名称   类型    一句话简介   详细简介
           return  table.Where(p => p.Mv_name.Contains(key)
               || p.Mv_type.Contains(key)
               || p.Mv_wordintroduce.Contains(key)
               || p.Mv_introduce_.Contains(key))
                .OrderBy(p => p.Mv_uptime).ToList();
        }
        public bool Addclick(int id)
        {
            var record = IdGetMovies(id);
            record.Mv_click++;
            return MV.SaveChanges()==1;
        }
        public List<Movies> TypeGetMovies(string type, int n)
        {
            return table.Where(p => p.Mv_type == type).OrderBy(p=>p.Mv_uptime).Take(n).ToList();
        }
        public bool AlertIntroduce(string a, int id)
        {
            var record = table.FirstOrDefault(p => p.Mv_id == id);
            if (record == null)
                return false;
            record.Mv_introduce_ = a;
            return MV.SaveChanges() == 1;
        }
        public List<Movies> GetAllrecord()
        {
            return table.OrderBy(p => p.Mv_uptime).ToList();
        
        }
    }
}
